﻿Module Registry

  Const BaseKey As String = "\Software\Avian Waves\ReRemind"

  Function GetRegValueBool(ByVal ValueName As String, ByVal DefaultValue As Boolean) As Boolean
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(BaseKey)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As Integer = Integer.Parse(.GetValue(ValueName).ToString)
            If x = 0 Then
              Return False
            ElseIf x <> 1 Then
              .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
            End If
            Return True
          Catch
          End Try
        End If

        If DefaultValue Then
          .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
        Else
          .SetValue(ValueName, 0, Microsoft.Win32.RegistryValueKind.DWord)
        End If
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Sub SetRegValueBool(ByVal ValueName As String, ByVal Value As Boolean)
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(BaseKey)
        If Value Then
          .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
        Else
          .SetValue(ValueName, 0, Microsoft.Win32.RegistryValueKind.DWord)
        End If
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub

  Function GetRegValueInt(ByVal ValueName As String, ByVal DefaultValue As Integer) As Integer
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(BaseKey)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As Integer = Integer.Parse(.GetValue(ValueName).ToString)
            Return x
          Catch
          End Try
        End If

        .SetValue(ValueName, DefaultValue, Microsoft.Win32.RegistryValueKind.DWord)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Sub SetRegValueInt(ByVal ValueName As String, ByVal Value As Integer)
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(BaseKey)
        .SetValue(ValueName, Value, Microsoft.Win32.RegistryValueKind.DWord)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub

  Function GetRegValueString(ByVal ValueName As String, ByVal DefaultValue As String) As String
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(BaseKey)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As String = .GetValue(ValueName).ToString
            Return x
          Catch
          End Try
        End If

        .SetValue(ValueName, DefaultValue, Microsoft.Win32.RegistryValueKind.String)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Sub SetRegValueString(ByVal ValueName As String, ByVal Value As String)
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(BaseKey)
        .SetValue(ValueName, Value, Microsoft.Win32.RegistryValueKind.String)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub

End Module
